#include "pch.h"
#include "framework.h"
#include "JniReadFile.h"
#include "readFile.h"
#include <string>

JNIEXPORT jlong JNICALL Java_JniReadFile_readFile(JNIEnv* env, jclass, jstring jStr) {

	int nRet = 0;
	if (!jStr)
		return nRet;

	const jclass stringClass = env->GetObjectClass(jStr);
	const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
	const jbyteArray stringJbytes = (jbyteArray)env->CallObjectMethod(jStr, getBytes, env->NewStringUTF("UTF-8"));

	size_t length = (size_t)env->GetArrayLength(stringJbytes);
	jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);

	std::string ret = std::string((char*)pBytes, length);
	env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);

	env->DeleteLocalRef(stringJbytes);
	env->DeleteLocalRef(stringClass);

	nRet = readFile(ret.c_str());
	return nRet;
}